 % Copyright (C) 2012 	Paul Bovbel, paul@bovbel.com
 % 						Richard Abrich, abrichr@gmail.com
 %
 % This file is part our empirical study of boosting algorithms (http://code.google.com/p/boosting-study/)
 % 
 % This is free software; you can redistribute it and/or modify
 % it under the terms of the GNU General Public License as published by
 % the Free Software Foundation; either version 3 of the License, or
 % (at your option) any later version.
 % 
 % This source code is distributed in the hope that it will be useful,
 % but WITHOUT ANY WARRANTY; without even the implied warranty of
 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 % GNU General Public License for more details.
 % 
 % You should have received a copy of the GNU General Public License
 % along with this source code. If not, see http://www.gnu.org/licenses/

function [ output ] = perceptron_online( data, parameters, train)
%PERCEPTRON train or test perceptron
%   data - input data
%   parameters - either initial training weights, or classifier parameters
%   for testing
%   mode - 'train', or 'test'
%   Variable Output:
%       -Learned parameters from training mode
%       -Classifier output for testing mode
%   mismatch - match/notmatch data

%learning rate for perceptron
eta = 0.001;
%regularization constant
alpha = 100;

N = size(data,1);
W = parameters;
A = zeros(1,N);

for n=1:N
    X = [1 data(n,2:end)];
    a = W .* X;

    %sigmoid activation:
    %A(i) = 2/(1+exp(-sum(a))-1;
    
    %linear activation:   
    A(n) = sum(a);
    
    %cap the linear activation:
    if A(n) > 1
        A(n) = floor(A(n));
    elseif A(n) < -1
        A(n) = ceil(A(n));
    end

    %step activation:
    %A(i) = sign(sum(a));
        
    T = data(n,1);
    
    %in training mode, update W
    if train == true
        delta = (T - A(n));
        dW = delta * X;
        W = W + eta * (dW - sign(W).*alpha.*W.^2);     
    end
end

%in training mode, output W
if train == true
    output = W;
%in testing mode, output activations
else
    output = A;
end
